package com.woniuxy.school.online.repair.util;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.commons.core.util.JwtUtil;
import com.woniuxy.school.online.repair.entity.RepairAttachment;
import io.jsonwebtoken.Claims;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.*;



/**
 * 上传文件工具类
 */
public class UploadUtil {
    private static final String pictureAddress = "E:/study/vue/special_sp/static/"; //图片存放位置

    /**
     * 上传多张图片
     * id          bigint auto_increment comment 'ID'
     * //        primary key,
     * //        repairNum   varchar(20)  null comment '报修编号',
     * //                uploadType  varchar(20)  null comment '类型',
     * //                oldFileName varchar(50)  null comment '原文件名',
     * //                fileName    varchar(50)  null comment '现文件名',
     * //                size        bigint       null comment '文件大小',
     * //                filePath    varchar(200) null comment '文件路径',
     * //                fileSuffix  varchar(20)  null comment '文件后缀',
     * //                createDate  datetime     null comment '上传时间',
     * //                uploder     varchar(234) null comment '上传人'
     *
     * @param images 所有文件
     * @return 返回文件地址
     */
    public static List<RepairAttachment> uploadImages(MultipartFile[] images, int userId, String repairNum) {
        ArrayList<RepairAttachment> list = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            RepairAttachment attachment = new RepairAttachment();
            //获取原文件名
            String originalFilename = images[i].getOriginalFilename();
            //获取文件后缀
            String suffix = originalFilename.substring(images[i].getOriginalFilename().lastIndexOf("."));
            //拼接文件地址
            String name = UUID.randomUUID() + suffix;
            String fileName = pictureAddress + name;
            File file = new File(fileName);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                    //将二进制数据存入文件
                    images[i].transferTo(file);
                    attachment.setRepairNum(repairNum);
                    attachment.setUploadType("img");
                    attachment.setOldFileName(originalFilename);
                    attachment.setFileName(name);
                    attachment.setSize(images[i].getSize());
                    attachment.setFilePath(fileName);
                    attachment.setFileSuffix(suffix);
                    attachment.setCreateDate(LocalDateTime.now());
                    attachment.setUploder(String.valueOf(userId));
                    list.add(attachment);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
//
//
//    /**
//     * 根据文件后缀查看文件的contentType
//     *
//     * @param fileExtension
//     * @return
//     */
//    private static String getContentType(String fileExtension) {
//        if (fileExtension == null) {
//            fileExtension = "";
//        }
//        //获取文件扩展名
//        ContentInfo mimeTypeMatch = ContentInfoUtil.findExtensionMatch(fileExtension);
//        String octetStreamValue = MediaType.APPLICATION_OCTET_STREAM_VALUE;//这是通用的
//        if (mimeTypeMatch != null) {
//            octetStreamValue = mimeTypeMatch.getMimeType();
//        }
//        return octetStreamValue;
//    }


    public static void main(String[] args) {
        String s = ",";
        for (int i = 0; i < 4; i++) {
            s += i + ",";
        }
        System.out.println(s);
    }
}
